package com.casic.producer;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 发送消息
 */
public class EProducerTopic {

    public static void main(String[] args) throws IOException, TimeoutException {
        //1.建立连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //2.设置参数
        factory.setHost("www.hqingq.top"); //默认localhost
        factory.setPort(5672); //默认5672
        factory.setVirtualHost("/hz");  //虚拟机  默认/
        factory.setUsername("hz"); //默认 guest
        factory.setPassword("hz"); //默认 guest
        //3.获取连接
        Connection connection = factory.newConnection();
        //4.创建channel（连接管道
        Channel channel = connection.createChannel();

        //5.创建交换机
        /*
        * exchangeDeclare(String exchange, BuiltinExchangeType type, boolean durable, boolean autoDelete, boolean internal, Map<String, Object> arguments)
        *   参数：
        *       1.exchange 交换机名称
        *       2.type 交换机类型
        *            DIRECT("direct"),把消息发送给符合指定routing key的队列
                     FANOUT("fanout"),广播，将消息发送给所有绑定到交换机的队列
                     TOPIC("topic"),通配符,
                     HEADERS("headers")参数匹配
        *       3.durable 是否持久化 是否持久化（mq重启后还在)
        *       4.autoDelete 是否自动删除，在没有消费者的情况下，自动删除
        *       5.internal 是否（MQ）内部使用，一般是false
        *       6.arguments 参数
        * */
        String exChangeName = "test_topic";
        channel.exchangeDeclare(exChangeName, BuiltinExchangeType.TOPIC,true,false,false,null);


        //6.创建队列
        String queueName1 = "test_topic_queue1";
        String queueName2 = "test_topic_queue2";
        /*
        * queueDeclare（String queue, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments）
        * */
        channel.queueDeclare(queueName1,true,false,false,null);
        channel.queueDeclare(queueName2,true,false,false,null);


        //7.绑定队列和交换机
        /*
        * queueBind(String queue, String exchange, String routingKey)
        *   参数：
        *       1.队列名称
        *       2.交换机名称
        *       3.路由键，绑定规则
        *           如果交换机类型为 fanout，key的值为""，证明发送给所有绑定的队列
        * */
        //routing key  系统名称.日志级别
        //所有error级别数据库存数据库、所有oeder系统日志错入数据库
        channel.queueBind(queueName1,exChangeName,"#.error");
        channel.queueBind(queueName1,exChangeName,"order.*");
        channel.queueBind(queueName2,exChangeName,"*.*");

        //8.发送消息
        //basicPublish(String exchange, String routingKey, BasicProperties props, byte[] body)
        String body = "日志信息：黄志调用给自己资产加一百万的方法，日志级别：error...";
        channel.basicPublish(exChangeName,"good.info",null,body.getBytes());

        //9.释放资源
        channel.close();
        connection.close();


    }
}
